package com.lxb.cmsservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxb.cmsservice.entity.CrmBanner;
import com.lxb.cmsservice.entity.Dto.QueryBannerDto;
import com.lxb.cmsservice.mapper.CrmBannerMapper;
import com.lxb.cmsservice.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 首页banner表 服务实现类
 *
 * @author lixuebin
 * @since 2022-12-11
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {


    /**
     * 获取轮播图数据
     *
     * @return
     */
    @Cacheable(value = "banner", key = "'selectIndexList'")
    @Override
    public List<CrmBanner> getAllList() {

        //根据sort进行降序排列，显示排列之后的前3条数据
        QueryWrapper<CrmBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("sort");
        //拼接语句
        queryWrapper.last("limit 3");
        List<CrmBanner> crmBanners = baseMapper.selectList(queryWrapper);
        return crmBanners;
    }

    /**
     * 多条件分页查询
     *  @param bannerPage
     * @param queryBannerDto
     * @return
     */
    @Override
    public Page<CrmBanner> pageBanner(Page<CrmBanner> bannerPage, QueryBannerDto queryBannerDto) {

        //查询条件
        QueryWrapper<CrmBanner> queryWrapper = new QueryWrapper<>();

        //判断多条件查询封装对象为空
        if (queryBannerDto != null) {
            String name = queryBannerDto.getName();
            String begin = queryBannerDto.getBegin();
            String end = queryBannerDto.getEnd();

            if (StringUtils.isNotEmpty(name)) {
                queryWrapper.like("title", name);
            }
            if (StringUtils.isNotEmpty(begin)) {
                queryWrapper.gt("gmt_create", begin);
            }
            if (StringUtils.isNotEmpty(end)) {
                queryWrapper.lt("gmt_modified", end);
            }
        }

        //排序
        queryWrapper.orderByDesc("sort");

        //实现分页
        baseMapper.selectPage(bannerPage, queryWrapper);

        return bannerPage;
    }
}
